//
//  TwERPEntity.m
//  TwERP
//
//  Created by Xiangyang Li on 12-7-17.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "TwERPEntity.h"
#import "TwERPJsonAgent.h"
#import <objc/runtime.h>

@implementation TwERPEntity


- (void) setValuesForKeysWithDictionary:(NSDictionary *)entityDict
{
    [super setValuesForKeysWithDictionary:entityDict];
    if(self.operationState ==0)
        self.operationState = GdBrowse;
}

- (id)initWithNewId
{
    self = [super init];
    if (self) {
        // Initialize self.
        //self.entityId = [TwERPJsonAgent idOfEntityType:[self class].description]; move to save operation
        self.entityId =0;
    }
    return self;
}

-(NSDate *) stringToDate:(NSString *)dateStr
{
    if(dateStr.length<5) 
    {
        return nil; 
    }
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSDate *date = nil;
    NSError *error = nil;
    NSRange r;
    r.location =0;
    r.length = 16;
    if ([dateFormatter getObjectValue:&date forString:dateStr range:&r error:&error])
        
        return date;
    else 
        return nil;
    
}
-(NSString *) dateToString:(NSDate *)date
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    return [dateFormatter stringFromDate:date];
}

- (NSArray *) allKeys {
    Class clazz = [self class];
    
    u_int count;
    
    objc_property_t* properties = class_copyPropertyList(clazz, &count);
    NSMutableArray* propertyArray = [NSMutableArray arrayWithCapacity:count];
    for (int i = 0; i < count ; i++) {
        const char* propertyName = property_getName(properties[i]);
        [propertyArray addObject:[NSString  stringWithCString:propertyName encoding:NSUTF8StringEncoding]];
    }
    free(properties);
    
    clazz = [self superclass];
    while (clazz != [TwERPEntity class]) {
        
        properties = class_copyPropertyList(clazz, &count);
        NSMutableArray* propertyArray0 = [NSMutableArray arrayWithCapacity:count];
        for (int i = 0; i < count ; i++) {
            const char* propertyName = property_getName(properties[i]);
            [propertyArray0 addObject:[NSString  stringWithCString:propertyName encoding:NSUTF8StringEncoding]];
        }
        free(properties);
        [propertyArray addObjectsFromArray:propertyArray0];
        clazz = [self superclass];

    }
    [propertyArray addObject:@"entityId"];
    [propertyArray addObject:@"operationState"];
    return [NSArray arrayWithArray:propertyArray];
}
-(NSDictionary *)transFormToDictionary
{
    [self validateEntityId];
    return [self dictionaryWithValuesForKeys:[self allKeys]];
}
-(void)validateEntityId
{
    if (self.entityId ==0)
        self.entityId = [TwERPJsonAgent idOfEntityType:[self class].description];    
}
-(NSString *) commit
{
   // NSLog(@"operstate:%d, gdeidt:%d,gdinsert:%d",self.operationState,GdEdit,GdInsert);
    [self validateEntityId];
 if(self.operationState == GdEdit || self.operationState == GdInsert)
 {
    NSMutableDictionary * gdDict =[[NSMutableDictionary alloc] initWithDictionary:[self transFormToDictionary]];
        [gdDict setValue:[[self class] description] forKey:@"entityName"];
      [gdDict setValue:[NSNumber numberWithInt:[TwERPJsonAgent getUserId]] forKey:@"userid"];
         return [TwERPJsonAgent commitEntity:gdDict];
 }
    return @"记录未修改，不需保存！";
    
}


@end
